cmake_minimum_required(VERSION 3.10)
project(QBasic)

include(CheckCXXCompilerFlag)

set(CMAKE_C_FLAGS_DEBUG   "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -Wall")

set(CMAKE_C_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE} -w")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -w")

CHECK_CXX_COMPILER_FLAG(-fvisibility-inlines-hidden COMPILER_HAS_VISIBILITY_INLINE_HIDDEN)
CHECK_CXX_COMPILER_FLAG(-fvisibility=hidden COMPILER_HAS_VISIBILITY_HIDDEN)
CHECK_CXX_COMPILER_FLAG(-std=c++11 LIBCXX_HAS_STDCXX11_FLAG)

if(${COMPILER_HAS_VISIBILITY_INLINE_HIDDEN})
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
endif(${COMPILER_HAS_VISIBILITY_INLINE_HIDDEN})

if(NOT MSVC)
    if(LIBCXX_HAS_STDCXX11_FLAG)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    else()
        CHECK_CXX_COMPILER_FLAG(-std=c++0x LIBCXX_HAS_STDCXX0X_FLAG)
        if(LIBCXX_HAS_STDCXX0X_FLAG)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
        else()
            if(NOT CMAKE_CXX_COMPILER MATCHES "icc")
                message(FATAL "need at least gcc 4.4.7 or clang 3.2")
            endif(NOT CMAKE_CXX_COMPILER MATCHES "icc")
        endif(LIBCXX_HAS_STDCXX0X_FLAG)
    endif(LIBCXX_HAS_STDCXX11_FLAG)
    
    if(CMAKE_SYSTEM_NAME MATCHES "Windows")
        set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -static")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
    endif(CMAKE_SYSTEM_NAME MATCHES "Windows")

endif(NOT MSVC)

option(USE_BISON "run bison" TRUE)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})

find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

if(MSVC)
    include_directories(${CMAKE_SOURCE_DIR}/getopt/)
    add_subdirectory(getopt)
endif(MSVC)

add_subdirectory(compiler)